package com.ycl.bookstore.utils;

import com.ycl.bookstore.DTO.UserDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断是否存在用户
        UserDTO userDTO = MyThreadLocal.getUser();
        //不存在用户就进行拦截
        if(userDTO==null){
            // 没有，需要拦截，设置状态码
            response.setStatus(401);
            return false;
        }
        return true;
    }

}
